﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using MarginTrading.Services;
using MarginTrading.Common;
using MarginTrading.Entity;

namespace MarginTrading.Forms
{
    public partial class PortfolioValuation : Form
    {
        public PortfolioValuation()
        {
            InitializeComponent();
        }

        private string GetClientCode()
        {
            string clientCode = string.Empty;
            if (!String.IsNullOrEmpty(txtBankAccNo.Text))
            {
                clientCode = ClientService.GetClientCodeBankAccNo(txtBankAccNo.Text);
            }
            else
            {
                if (!String.IsNullOrEmpty(txtClientName.Text))
                {
                    int startIndex = txtClientName.Text.LastIndexOf('(') + 1;
                    int length = txtClientName.Text.LastIndexOf(')') - txtClientName.Text.LastIndexOf('(') - 1;
                    if (length > 0)
                    {
                        clientCode = txtClientName.Text.Substring(startIndex, length);
                    }
                }
                else
                {
                    string strClientCode = "{0}-{1}-{2}-{3}";
                    clientCode = string.Format(strClientCode, txtClientIDPrefix.Text, txtClientID1.Text, txtClientID2.Text,
                        txtClientID3.Text);
                }
            }
            return clientCode;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            txtClientName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            AutoCompleteStringCollection clientNames = ClientService.GetClientNameCollection();
            AutoCompleteStringCollection bankAccNo = ClientService.GetBankAccNoCollection();

            if (clientNames != null)
            {
                txtClientName.AutoCompleteCustomSource = clientNames;
            }
            if (bankAccNo != null)
            {
                txtBankAccNo.AutoCompleteCustomSource = bankAccNo;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            if (!String.IsNullOrEmpty(GetClientCode()) && GetClientCode() != "---")
            {
                LoadReport(GetClientCode());
            }
            button1.Enabled = true;
        }

        private void LoadReport(string clientId)
        {
            try
            {
                Client client = ClientService.GetClientByCode(clientId);

                if (client == null)
                {
                    client = ClientService.GetClientByCode(clientId.ToUpper());
                    if (client == null)
                    {
                        client = new Client();
                    }
                    else
                    {
                        clientId = clientId.ToUpper();
                    }
                }

                ReportDataSource ds = new ReportDataSource();
                ds.Name = "master";
                ds.Value = ReportServices.GetPortfolioValuation1(clientId);


                this.reportViewer1.LocalReport.ReportPath = "PortfolioValuation1.rdlc";
                this.reportViewer1.LocalReport.DataSources.Clear();
                this.reportViewer1.LocalReport.DataSources.Add(ds);

                Config AgentCodeConfig = ConfigServices.GetConfigByName("Agent Code");
                string AgentCode = "cts";
                if (AgentCodeConfig != null)
                {
                    AgentCode = AgentCodeConfig.Value;
                }

                //TODO:Remove this 
                AgentCode = Reports.AGENT_CODE;

                this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] {
                        new ReportParameter("ImageLink",AgentCode.ToLower()),
                        new ReportParameter("CompanyName",Reports.COMPANY_NAME),
                        new ReportParameter("ClientID",client.Code),
                        new ReportParameter("BankAccNo",client.BankAccNo),
                        new ReportParameter("Name",string.Concat(client.Initials," ", client.Surname) ),
                        new ReportParameter("Address",string.Concat(client.Address_1," ",client.Address_2," ",client.Address_3," ",client.Address_4))});

                this.reportViewer1.RefreshReport();
                reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
                reportViewer1.ZoomMode = ZoomMode.Percent;
                reportViewer1.ZoomPercent = 100;
            }
            catch (Exception e)
            {

            }
        }

        private void txtClientName_TextChanged(object sender, EventArgs e)
        {
            txtClientIDPrefix.Text = string.Empty;
            txtClientID1.Text = string.Empty;
            txtClientID2.Text = string.Empty;
            txtClientID3.Text = string.Empty;
            txtBankAccNo.Text = string.Empty;
        }

        private void txtClientIDPrefix_TextChanged(object sender, EventArgs e)
        {
            txtClientName.Text = string.Empty;
            txtBankAccNo.Text = string.Empty;
            if (txtClientIDPrefix.Text.Length == 3)
            {
                txtClientID1.Focus();
            }
        }

        private void txtClientID1_TextChanged(object sender, EventArgs e)
        {
            txtClientName.Text = string.Empty;
            txtBankAccNo.Text = string.Empty;
            if (txtClientID1.Text.Length == 9)
            {
                txtClientID2.Focus();
            }
        }

        private void txtClientID2_TextChanged(object sender, EventArgs e)
        {
            txtClientName.Text = string.Empty;
            txtBankAccNo.Text = string.Empty;
            if (txtClientID2.Text.Length == 2)
            {
                txtClientID3.Focus();
            }
        }

        private void txtClientID3_TextChanged(object sender, EventArgs e)
        {
            txtClientName.Text = string.Empty;
            txtBankAccNo.Text = string.Empty;
        }

        private void txtBankAccNo_TextChanged(object sender, EventArgs e)
        {
            txtClientIDPrefix.Text = string.Empty;
            txtClientID1.Text = string.Empty;
            txtClientID2.Text = string.Empty;
            txtClientID3.Text = string.Empty;
            txtClientName.Text = string.Empty;
        }
    }
}
